//
//  SingleChoseMenu.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/1/25.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SingleChoseMenu.h"
@interface SingleChoseMenu ()
@property(nonatomic,strong)void(^complect)(NSInteger index);
@property(nonatomic,assign)NSInteger selectedIndex;
@property(nonatomic,assign)NSInteger count;
@property(nonatomic,strong)NSArray * colorArray;
@property(nonatomic,strong)UIColor * defaultColor;
@property(nonatomic,assign)BOOL ifCancle;
@end

@implementation SingleChoseMenu

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles XInterval:(NSInteger )XInterval YInterval:(NSInteger )YInterval defaultColor:(UIColor *)defaultColor colors:(NSArray *)colors cancleSelect:(BOOL)cancleSelect complect:(void(^)(NSInteger index))complect{
    self = [super initWithFrame:frame];
    if (self) {
        _complect = complect;
        _ifCancle = cancleSelect;
        _selectedIndex = titles.count;
        _count = titles.count;
        _colorArray = colors;
        _defaultColor = defaultColor;
        XInterval = XInterval*VerticalRatio();
        YInterval = YInterval * VerticalRatio();
        int numbery = 0;
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat width = (frame.size.width - 4*XInterval)/3;
        CGFloat height = 25*VerticalRatio();
        for (int i = 0; i<titles.count; i++) {
            numbery = i/3;
            y = (25+YInterval)*numbery;
            x = XInterval + (width+XInterval) *(i%3);
            UIButton * button =[UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(x, y, width, height);
            button.backgroundColor =[UIColor colorWithWhite:0.937 alpha:1.000];
            [button setTitle:titles[i] forState:UIControlStateNormal];
            button.titleLabel.font =[UIFont systemFontOfSize:14*VerticalTexeRatio()];
            if (colors.count>0) {
                [button setTitleColor:colors[i] forState:UIControlStateNormal];
            }else{
            [button setTitleColor:defaultColor forState:UIControlStateNormal];
            }
            
            button.tag = 100+i;
            [button addTarget:self action:@selector(changeIndex:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
           
        }
        
         self.contentSize = CGSizeMake(frame.size.width, y+height+1);
    }
    return self;
}

- (void)changeIndex:(UIButton *)sender{
    if (_ifCancle) {
         sender.selected = !sender.selected;
    }
   
    if (sender.tag == _selectedIndex) {
        if (_ifCancle) {
            if (sender.selected) {
                sender.selected = YES;
                sender.layer.borderColor = [UIColor colorWithRed:1.000 green:0.710 blue:0.082 alpha:1.000].CGColor;
                sender.layer.borderWidth = 1;
                sender.backgroundColor = [UIColor whiteColor];
                _complect(sender.tag-100);
            }else{
                [sender setTitleColor:_defaultColor forState:UIControlStateNormal];
                sender.layer.borderColor = [UIColor clearColor].CGColor;
                sender.layer.borderWidth = 0;
                sender.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
                
                _complect(-1);
            }
        }
      
        return;
    }
    sender.selected = YES;
    sender.layer.borderColor = [UIColor colorWithRed:1.000 green:0.710 blue:0.082 alpha:1.000].CGColor;
    sender.layer.borderWidth = 1;
    [sender setTitleColor:[UIColor colorWithRed:1.000 green:0.710 blue:0.082 alpha:1.000] forState:UIControlStateSelected];
    sender.backgroundColor = [UIColor whiteColor];
    
    if (_count!=_selectedIndex) {
        UIButton * button = (UIButton *)[self viewWithTag:_selectedIndex];
        button.selected = NO;
        button.layer.borderColor = [UIColor clearColor].CGColor;
        button.layer.borderWidth = 0;
        if (_colorArray.count>0) {
            [button setTitleColor:_colorArray[_selectedIndex-100] forState:UIControlStateSelected];
        }else{
        [button setTitleColor:_defaultColor forState:UIControlStateSelected];
        }
        
        button.backgroundColor = [UIColor colorWithWhite:0.937 alpha:1.000];
    }
    _selectedIndex = sender.tag;
    _complect(sender.tag-100);

}


@end
